<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架 [ 以下开发工具自动生成-版权勿删 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace addons\cloud_market\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * 问题回答模型文件
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 */
class CloudMarketQuestionAnswer extends CloudMarketBaseModel
{
    // 配置软删除、启用回收站
    use SoftDelete;
    protected $deleteTime = "delete_time";

    // 字段自动类型转换
    protected $type = array(

    );
    // 数据输出需要追加的属性
    protected $append = array(
        'user',
        'reply_user',
        'comments',
        'comments_count',
        'human_time',
        'summary'
    );
    public function __construct(array $data = [])
    {
        parent::__construct($data);
    }


    /**
     * 关联CloudMarketUser模型
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(CloudMarketUser::class, 'user_id', 'id')->field(['id','username','nickname','avatar']);
    }

    /**
     * 关联CloudMarketUser模型
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\model\relation\BelongsTo
     */
    public function replyUser()
    {
        return $this->belongsTo(CloudMarketUser::class, 'reply_user_id', 'id')->field(['id','username','nickname','avatar']);
    }

    /**
     * 关联CloudMarketQuestion模型
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\model\relation\BelongsTo
     */
    public function question()
    {
        return $this->belongsTo(CloudMarketQuestion::class, 'question_id', 'id');
    }

    public function getContentAttr($val,$data){
        return htmlspecialchars_decode($val);
    }

//    protected $append = [
//        'user',
//        'reply_user',
//        'human_time',
//        'comments',
//        'comments_count',
//        'summary'
//    ];

    public static function onAfterInsert(Model $model)
    {
        self::updateAnswerCount($model['question_id']);
    }

    public static function onAfterDelete(Model $model){
        self::updateAnswerCount($model['question_id']);
    }
//
    public static function updateAnswerCount($question_id){
        $answer_count = self::where('question_id', $question_id)->count();
        $question = (new CloudMarketQuestion())->find($question_id);
        if($question){
            $question->answer_count = $answer_count;
            $question->save();
        }
    }

    // 获取精简后的内容
    public function getSummaryAttr($value, $data)
    {
        return mb_substr(strip_tags(htmlspecialchars_decode($data['content'])), 0, 200);
    }

    // 获取回答评论
    public function getCommentsAttr($value,$data){
        return $this->where('answer_id',$data['id'])->order('id','desc')->limit(0,3)->select();
    }

    // 获取评论总数
    public function getCommentsCountAttr($value,$data){
        return $this->where('answer_id',$data['id'])->count();
    }

//    // 回答用户信息
//    public function user()
//    {
//        return $this->belongsTo('app\api\model\User', 'user_id', 'id')
//            ->field(['avatar', 'username', 'id'])
//            ->joinType('LEFT')
//            ->append(['url']);
//    }
//
//    // 被回复用户信息
//    public function replyUser(){
//        return $this->belongsTo('app\api\model\User', 'reply_user_id', 'id')
//            ->field(['avatar', 'username', 'id'])
//            ->joinType('LEFT')
//            ->append(['url']);
//    }


    // 获取人性化时间
    public function getHumanTimeAttr($value,$data){
        return human_time($data['create_time']);
    }
}
